/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frm.creditControl.session;

import crm.clientManagement.entity.CompanyEntity;
import frm.creditControl.entity.CreditAccountEntity;
import frm.creditControl.util.exception.CreditAccountFrozenException;
import frm.creditControl.util.exception.CreditLimitExceededException;
import frm.creditControl.util.exception.FinanceException;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import crm.salesOrderProcessing.entity.SalesOrderEntity;
import crm.salesOrderProcessing.util.exception.IllegalCompanyTypeException;
import commonInfrastructure.system.entity.SystemParameterEntity;
import commonInfrastructure.system.session.SystemSessionBean;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author ReinForceII
 */
@Stateless
@LocalBean
public class CreditControlSessionBean {

    @PersistenceContext
    private EntityManager em;
    @EJB
    private SystemSessionBean systemSessionBean;

    public CreditAccountEntity createNewCreditAccount(CompanyEntity company) throws IllegalCompanyTypeException {

        CreditAccountEntity newAccount = new CreditAccountEntity();

        SystemParameterEntity systemParameter = systemSessionBean.getSystemParameter();
        if (company.getCompanyType().equals(systemParameter.getDS_COMPANY_TYPE())) {
            newAccount.setCreditLimit(systemParameter.getDirectSalesCreditLimit());
        } else if (company.getCompanyType().equals(systemParameter.getWS_COMPANY_TYPE())) {
            newAccount.setCreditLimit(systemParameter.getWholeSalesCreditLimit());
        } else {
            throw new IllegalCompanyTypeException();
        }

        newAccount.setCompany(company);
        newAccount.setCurrentBalance(0);
        newAccount.setFrozen(false);


        return newAccount;
    }

    public void creditCheck(SalesOrderEntity salesOrder) throws FinanceException {

        CompanyEntity company = salesOrder.getCustomer();
        CreditAccountEntity creditAccount = company.getCreditAccount();

        if (creditAccount == null) {
            System.out.println("==================================================Credit account not found, CreditControlSessionBean");
            return;
        }

        if (creditAccount.isFrozen()) {
            throw new CreditAccountFrozenException();
        }

        if (salesOrder.getTotalAmount() > (creditAccount.getCreditLimit() + creditAccount.getCurrentBalance())) //Here creditLimit is a positive number and currentBalance should be a negative number if the company owes our money. Thus the sum of the two gives us how much credit is left in this account.
        {
            throw new CreditLimitExceededException("Amount available in this account: " + (creditAccount.getCreditLimit() + creditAccount.getCurrentBalance()));
        }
        
        creditAccount.setCurrentBalance(creditAccount.getCurrentBalance() - salesOrder.getTotalAmount());
        saveChanges(creditAccount);
        


    }
    
    public boolean saveChanges(CreditAccountEntity creditAccount){
        
        em.merge(creditAccount);
        
        return true;
    }
    
    public boolean addCreditAccount(CreditAccountEntity creditAccount){
        
        em.persist(creditAccount);
        
        return true;
    }
    
    public List<CreditAccountEntity> getAllCreditAccount(){
        
        Query query = em.createQuery("SELECT cae FROM CreditAccountEntity cae");
        return query.getResultList();
    }
}
